import Vue from 'vue'
import Vuex from 'vuex'
import {apiMerchantIndex,apiShopTagInfo} from '../request/api.js'
Vue.use(Vuex)

const store = new Vuex.Store({
    state:{
      userInfo: {
        token: '',
        name: '',
        password: ''
      },
      userConfigInfo:{
        showNoticeModal: 1
      },
      merchantInfo: {},
      shopNameData: [],
      platformType: '',
      keepAlive: []
    },
    getters:{
      userToken(state){
        return window.localStorage.getItem('token')
      },
      userName(state){
        return  window.localStorage.getItem('phone')
      },
      userPassword(state){
        return indow.localStorage.getItem('password')
      },
      showNoticeModalStatus(state){
        return JSON.parse(window.localStorage.getItem('userConfigInfo')).showNoticeModal
      },
      shopNameList(state){
        return state.shopNameData
      },
      platformType(state){
        return state.platformType
      },
      keepAlive(state){
        return state.keepAlive
      }
    },
    mutations:{
        SET_USERINFO(state,data){
          let newData = JSON.stringify(data)
          state.userInfo = data
          window.localStorage.setItem('userInfo', newData);
        },
        SET_USERCONFIG(state,data){
          let newData = JSON.stringify(data)
          state.userConfigInfo = data
          window.localStorage.setItem('userConfigInfo', newData);
        },
        SET_SHOP_NAME_LIST(state,data){
          state.shopNameData = data
        },
        SET_PLATFORMTYPE_TYPE(state,data){
          state.platformType = data
        },
        SET_KEEP_ALIVE: (state, keepAlive) => {
          state.keepAlive.push(keepAlive)
        },
        DELETE_KEEP_ALIVE(state,data){
          state.keepAlive.splice(data,1)
        },
    },
    actions:{
      setKeepAlive({commit},data){
        commit('SET_KEEP_ALIVE',data)
      },
      deleteKeepAlive({commit},data){
        commit('DELETE_KEEP_ALIVE',data)
      },
      setPlatformType({commit},data){
         commit('SET_PLATFORMTYPE_TYPE',data)
      },
      setUserInfo({commit},data){
         commit('SET_USERINFO',data)
      },
      setUserConfig({commit},data){
         commit('SET_USERCONFIG',data)
      },
      setShopNameList({commit},data){
        this.$isClick(true);
        apiShopTagInfo({
          token: window.localStorage.getItem('token')
        }).then(res =>{
          this.$isClick(false);
           if (res.code == 1) {
              let storeName = {
                1: '淘宝',
                2: '天猫',
                3: '京东',
                13: '唯品会',
                15: '拼多多'
              }
              let store = []
              for (var i in res.msg.shop_list) {
                store.push({
                  name: storeName[res.msg.shop_list[i].type] + '-' + res.msg.shop_list[i].name,
                  type: res.msg.shop_list[i].type,
                  id: res.msg.shop_list[i].id
                })
              }
             commit('SET_SHOP_NAME_LIST',store)

           }
        }).catch(res => {

          this.$isClick(false);
        })
      }
    }
});
export default store;
